package edu.whcp.comp2301.controller;

import edu.whcp.comp2301.dao.CategoryDao;
import edu.whcp.comp2301.dao.DashboardDao;
import edu.whcp.comp2301.dao.NewsDao;
import edu.whcp.comp2301.pojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/dashboard")
public class DashboardController {
    @Autowired
    private NewsDao newsDao;

    @Autowired
    private DashboardDao dashboardDao;

    @Autowired
    private CategoryDao categoryDao;

    @RequestMapping
    public ModelAndView recentNews() {
        List<News> newsList = newsDao.getNewsList();
        return new ModelAndView("backend/dashboard","newsList",newsList);
    }

    @RequestMapping(value = "/news/count",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer newsCount() {
        return dashboardDao.getNewsCount();
    }

    @RequestMapping(value = "/news/percent",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer newsPercent() {
        return dashboardDao.getNewsCountPercent();
    }

    @RequestMapping(value = "/users/count",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer userCount() {
        return dashboardDao.getUserCount();
    }

    @RequestMapping(value = "/users/percent",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer userPercent() {
        return dashboardDao.getUserCountPercent();
    }

    @RequestMapping(value = "/comments/count",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer commentCount() {
        return dashboardDao.getCommentCount();
    }

    @RequestMapping(value = "/comments/percent",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer commentPercent() {
        return dashboardDao.getCommentCountPercent();
    }


    @RequestMapping(value = "/category/count",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public int getCategoryCount(){
        return categoryDao.getAllCategories().size();
    }
}
